import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.Motor;
import lejos.nxt.comm.Bluetooth;
import lejos.nxt.comm.NXTConnection;
import lejos.robotics.navigation.DifferentialPilot;
import lejos.util.Delay;


public class evasion {
	
	static DifferentialPilot piloto = new DifferentialPilot(4f, 13.4f, Motor.A, Motor.C);
	void evitar(){
		
            
            LCD.clear();
            LCD.drawString("'evasion'", 0, 7);
            piloto.stop();
            piloto.travel(-15);
            piloto.rotate(45);
            Delay.msDelay(2000);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		evasion ev = new evasion();
		String connected = "Conectado";
	    String waiting = "Esperando...";
	    String closing = "Cerrando...";
	   int contador=0;
	    	
	    while(contador==0){
	    	boolean s;
	      LCD.drawString(waiting,0,0);
	      NXTConnection connection = Bluetooth.waitForConnection(); 
	      LCD.clear();
	      LCD.drawString(connected,0,0);
	     

	      DataInputStream dis = connection.openDataInputStream();
	      DataOutputStream dos = connection.openDataOutputStream();
	      piloto.forward();
         try{
	       s = dis.readBoolean() ;      
          }catch(IOException ioe) {
      	    LCD.drawString("error capturando", 0, 0);
          }
          
	      if (s=true){
	    	  LCD.drawString("recibido", 1, 4);
	    	  piloto.stop();
	  	 	piloto.travel(-15);
	  	 	piloto.rotate(45);
	         try{
	          dos.writeChars("k");
	          dos.flush();
	         }catch(IOException ioe) {
	       	    LCD.drawString("error capturando", 0, 0);
	           }
	      }
	      else {
	    	  LCD.drawString("no se recibio", 1, 4);
	      }
	      
	      try{
	      dis.close();
	      dos.close();
	      } catch (IOException e) {
			    LCD.drawString("Close Exception", 0, 5);
			     LCD.refresh();
		 }
	      LCD.clear();
	      LCD.drawString(closing,0,0);

	      connection.close();
	      LCD.clear();
	      contador=1;
	  
	      }
	    Button.waitForAnyPress();
	    }
	    
	     
	  }
